/*
 *description:signup page
 *data:2015-05-06
 *author:yebanliuying@live.com
*/
	//弹框
    function tips_box(pop_box,close,bomb_width,bomb_height,title_text){	//框id, 关闭按钮id, 宽, 高, 文案
        var screen_w   = $(window).width()/2 - bomb_width/2;
        var screen_h   = $(window).height()/2 - bomb_height/2;
        var bomb_w     = bomb_width;	//弹框宽
        var bomb_h     = bomb_height;
        var bomb_html  = "<div id='"+pop_box+"' class='bomb_box' style='width:"+bomb_w+"px;height:"+bomb_h+"px;left:"+screen_w+"px;top:-"+bomb_h+"px';><p>"+title_text+"</p><div class='yy_container'><button id='"+close+"' class='yy_btn yy_btn_success yy_btn_block close_bomb' style='height:36px;' >关闭</button></div></div>";
        var mask_html  = "<div id='mask'></div>";
        var close_bomb = $("#"+close);	//关闭弹框元素
        var bomb_box   = $("#"+pop_box);	//弹框主体元素
        var mask       = $("#mask");	//遮罩层元素
        
        if ($("#mask").length == 0) {
        	$("body").append(mask_html);
        }
        
        if(bomb_box.length == 0){
            $("body").append(bomb_html);
        }
        

		$("#mask").fadeIn(200);
        $("#"+pop_box).animate({
            top: (screen_h+10)+"px",
            left: screen_w+"px",
            width: bomb_w+"px",
            height: bomb_h+"px"
        },300);
        $("#"+pop_box).animate({
            top: screen_h+"px",
            left: screen_w+"px",
            width: bomb_w+"px",
            height: bomb_h+"px"
        },160);

        $("#"+close).click(function(){
        	$("#mask").fadeOut(200);
            $("#"+pop_box).animate({
	            top: "-"+bomb_h+"px",
	            left: screen_w+"px",
	            width: bomb_w+"px",
	            height: bomb_h+"px"
	        },200);
        })
    }

var html,input,box,btn_get_code,Atime,tel_num,get_success,invitation_code;
	html       =  "<div class=\"yy_input_delete\" style='display:none;'><i class=\"yy_icon_remove\"></i></div>";
	input      =  $(".yy_input_delete_wrap input");
	box        =  $(".yy_input_delete_wrap");
	btn_delete =  $(".yy_input_delete");	//删除按钮
	tel_num    =  $("#tel_num");	//电话输入框
	code_input =  $("#code_input");	//验证码输入框
	invitation_code = $("#invitation_code");//邀请码
	btn_get_code = $("#btn_get_code");	//验证码按钮
	btn_get_start = $("#btn_get_start");	//开始按钮
	get_success   = true;	//ajax 成功后 修改这个值，验证是否成功 默认false

/*执行代码*/
$(function(){

	input.parent(box).append(html);

	input.focus(function(){
		var input_this = $(this);
		console.log(input_this);
		Atime = setInterval(function(){
			if(input_this.val().length > 0){//当输入框有值
				input_this.parent(box).children(".yy_input_delete").show();
				if(tel_num.val().length == 11 && (btn_get_code.text() == "获取验证码" || btn_get_code.text() == "重新获取")){
					btn_get_code.attr("disabled", false);
				}else{
					btn_get_code.attr("disabled", true);
				}
				if(code_input.val().length == 6 && get_success && invitation_code.val().length > 10){//验证码 true
					btn_get_start.attr("disabled", false);
				}else{
					btn_get_start.attr("disabled", true);
				}
			}else{

				$(".yy_input_delete").hide();

			}
		},200);

	});
	var reg = /^d{0,1}\d*$/;
	input.keyup(function(){
		var input_this = $(this);
		var val = input_this.val();
		if(!reg.test(val)){
			if(/^.*\d$/.test(val)){
                input_this.val(val.trim());
			}else{
                val = val.substr(0,val.length-1);
                input_this.val(val);
            }
		}
	});
	input.blur(function(){
		//清除定时器
		clearInterval(Atime);

		setTimeout(function(){$(".yy_input_delete").hide()}, 200);

	});

	$(".yy_input_delete").click(function(){

		$(this).parent(box).children("input").val('').focus();
		$(".yy_input_delete").hide();

		if(tel_num.val().length !== 11){
			btn_get_code.attr("disabled", true);
		}
		if(code_input.val().length !== 6){
			btn_get_start.attr("disabled", true);
		}
	});
	function sendMsg(tel){
		return $.ajax({
			url : "send-invitation",
			data:{"mobile":tel},
			async : false
		});
	}

	var	countdown = 120;//倒计时控制
	btn_get_code.click(function(){
		var idRandom = Math.round(Math.random()*10000);
		function settime() {
			if (countdown == 0) {
				btn_get_code.attr("disabled", false);
				tel_num.attr("disabled", false);
				btn_get_code.html("重新获取");
				countdown = 120;//倒计时控制
			} else {
				if (tel_num.val().length < 11) {
					clearTimeout(Btime);
					tel_num.attr("disabled", false);
					btn_get_code.html("重新获取");
					countdown = 120;//倒计时控制
				}else{
					btn_get_code.attr("disabled", true);
					tel_num.attr("disabled", true);
					btn_get_code.html("重新发送(" + countdown + ")");
					countdown--;
					var Btime= setTimeout(settime ,1000);
				}
			}
		}
		var tel = tel_num.val();
		var reg =  /^0?1[3|4|5|7|8][0-9]\d{8}$/;
		if (reg.test(tel)) {
			res = sendMsg(tel);
			console.log(res.responseText);
			var responseObj = JSON.parse(res.responseText);
			if(responseObj.success){
				settime();
			}else{
				var result = responseObj.result;
				console.log(result);
				var msg;
				if(undefined === result || null === result){
					msg = "注册失败，请稍后再试";
				}else{
					msg = result.errMsg;
					msg = (undefined == msg ?"注册失败，请稍后再试":msg);
				}
				tips_box("test_box"+idRandom,"test_btn"+idRandom,"180","90",msg);
			}
		}else{
			 tips_box("test_box"+idRandom,"test_btn"+idRandom,"180","90","手机号码错误");
		}

	});
	btn_get_start.click(function(){
		var idRandom = Math.round(Math.random()*10000);
		$.post("sign-up",{
			mobile:tel_num.val(),
			invitationCode:invitation_code.val(),
			identifyCode:code_input.val()
		},function(data){
			console.log(data);
			//var result = JSON.parse(data);
			//console.log(result);
			if(data["success"]){
				window.location.href="sign-up-success";
			}else{
				var result = data.result;
				console.log(result);
				var msg;
				if(undefined === result || null === result){
					msg = "注册失败，请稍后再试";
				}else{
					msg = result.errMsg;
					msg = (undefined == msg ?"注册失败，请稍后再试":msg);
				}
				tips_box("test_box"+idRandom,"test_btn"+idRandom,"180","90",msg);
			}
		});
		//window.location.href="/sign-up-success";
	});

});
